package com.dhcc.bpm.modules.sandbox.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.orga.service.IAppApiService;
import com.dhcc.bpm.modules.orga.vo.AppApiVO;
import com.dhcc.bpm.modules.sandbox.entity.SandboxCase;
import com.dhcc.bpm.modules.sandbox.mapper.SandboxCaseMapper;
import com.dhcc.bpm.modules.sandbox.service.ISandboxApiService;
import com.dhcc.bpm.modules.sandbox.service.ISandboxCaseService;
import com.dhcc.bpm.modules.sandbox.vo.SandboxApiVO;
import com.dhcc.bpm.modules.sandbox.vo.SandboxCaseVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName SandboxCaseServiceImpl
 * @Description 沙箱实例表 服务实现类
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@Service
public class SandboxCaseServiceImpl extends BaseServiceImpl<SandboxCaseMapper, SandboxCase> implements ISandboxCaseService {
	private final static Logger log = LoggerFactory.getLogger(SandboxCaseServiceImpl.class);

	@Autowired
	private ISerialNumberService serialNumberService;

	@Autowired
	private IAppApiService appApiService;

	@Autowired
	private ISandboxApiService sandboxApiService;

	/**
	 * 自定义分页
	 * @Author sjm
	 * @Date 18:20 2020/2/21 0021
	 * @param page
	 * @param sandboxCase
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.vo.SandboxCaseVO>
	 **/
	@Override
	public IPage<SandboxCaseVO> selectSandboxCasePage(IPage<SandboxCaseVO> page, SandboxCaseVO sandboxCase) {
		return page.setRecords(baseMapper.selectSandboxCasePage(page, sandboxCase));
	}

	/**
	 * 沙箱管理新增或修改
	 * @Author sjm
	 * @Date 18:20 2020/2/21 0021
	 * @param sandboxCase
	 * @return boolean
	 **/
	@Override
	public boolean modifySandboxCase(SandboxCase sandboxCase) {
		try {
			if (sandboxCase.getId() == null) {
				String date = serialNumberService.generateSerialNumberByModelCode("sandbox_case_date");
				String num = serialNumberService.generateSerialNumberByModelCode("sandbox_case_num");
				String code = date.substring(0, date.length() - 1) + num;
				sandboxCase.setCaseCode(code);
			}
			saveOrUpdate(sandboxCase);
			List<AppApiVO> list = appApiService.findApiListByAppId(sandboxCase.getOpcAppId());
			for (AppApiVO appApiVO : list) {
				SandboxApiVO sandboxApi = sandboxApiService.selectSandboxApiList(sandboxCase.getId(), appApiVO.getOpcAppId());
				if (sandboxApi != null) {
					sandboxApi.setApiId(appApiVO.getOpcApiId());
				} else {
					sandboxApi = new SandboxApiVO();
					sandboxApi.setApiId(appApiVO.getOpcApiId());
					sandboxApi.setCaseId(sandboxCase.getId());
				}
				sandboxApiService.saveOrUpdate(sandboxApi);
			}
			return true;
		}catch (Exception e){
			log.error("沙箱案例执行异常：{}", e);
			return false;
		}
	}

	@Override
	public SandboxCase selectSandboxCaseByAppId(Long appId) {
		return baseMapper.selectSandboxCaseByAppId(appId);
	}


}
